package com.greyu.ysj.service.impl;

import com.github.pagehelper.PageHelper;
import com.greyu.ysj.config.Constants;
import com.greyu.ysj.config.ResultStatus;
import com.greyu.ysj.entity.CbtArea;
import com.greyu.ysj.entity.CbtAreaExample;
import com.greyu.ysj.entity.CbtInvest;
import com.greyu.ysj.entity.CbtStarLevel;
import com.greyu.ysj.entity.User;
import com.greyu.ysj.entity.UserExample;
import com.greyu.ysj.mapper.CbtAreaMapper;
import com.greyu.ysj.mapper.CbtInvestMapper;
import com.greyu.ysj.mapper.UserMapper;
import com.greyu.ysj.model.ResultModel;
import com.greyu.ysj.service.CbtInvestService;
import com.greyu.ysj.service.CbtMinerRatioService;
import com.greyu.ysj.service.CbtStarLevelService;
import com.greyu.ysj.service.CbtUserUpdateMapperService;
import com.greyu.ysj.service.UserService;
import com.greyu.ysj.utils.CodeProcess;
import com.greyu.ysj.utils.NodeItem;
import com.greyu.ysj.utils.SubZero;
import com.greyu.ysj.utils.WalletAccount;
import com.greyu.ysj.utils.WalletProcess;

import org.apache.jasper.tagplugins.jstl.core.If;
import org.checkerframework.checker.units.qual.cd;
import org.eclipse.jdt.internal.compiler.codegen.MultiCatchExceptionLabel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
  
	@Autowired
    private UserMapper userMapper;
	@Autowired
	private CbtInvestMapper cbtInvestMapper;
	@Autowired
	private CbtMinerRatioService cbtMinerRatioService;
	@Autowired
	private  CbtStarLevelService cbtStarLevelService;
	@Autowired
	private CbtAreaMapper cbtAreaMapper;
    @Override
    public User selectUserById(int userId) {
    	return this.userMapper.selectByPrimaryKey(userId);
    }
    @Override
    public User selectByUserName(String username) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUserNameEqualTo(username);
        List<User> users = this.userMapper.selectByExample(userExample);

        User user;
        try {
            user = users.get(0);
        } catch (Exception e) {
            return null;
        }
        return user;
    }
    @Override
    public User selectByUserId(int userId) {
    	UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<User> users = this.userMapper.selectByExample(userExample);

        User user;
        try {
            user = users.get(0);
        } catch (Exception e) {
            return null;
        }
        return user;
    }
    @Override
    public User selectByPhone(String phone){
    	UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andPhoneEqualTo(phone);
        List<User> users = this.userMapper.selectByExample(userExample);

        User user;
        try {
            user = users.get(0);
        } catch (Exception e) {
            return null;
        }
        return user;
    }
	@Override
	public User selectByEmail(String email) {
		UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andMailNumEqualTo(email);
        List<User> users = this.userMapper.selectByExample(userExample);
        User user;
        try {
            user = users.get(0);
        } catch (Exception e) {
            return null;
        }
        return user;
	}

    
    @Override
    public User selectByAvatar(String avatar){
    	UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andAvatarEqualTo(avatar);
        List<User> users = this.userMapper.selectByExample(userExample);

        User user;
        try {
            user = users.get(0);
        } catch (Exception e) {
            return null;
        }
        return user;
    }
    @Override
    public int getUserIDbyPhone(String phone){
    	
    	UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andPhoneEqualTo(phone);
        List<User> users = this.userMapper.selectByExample(userExample);

        User user;
        try {
            user = users.get(0);
        } catch (Exception e) {
            return -1;
        }
        return user.getUserId();
    }

    @Override
    public List<User> getAllUsers(Integer page, Integer rows, User user) {
        if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
        
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();

        if (null != user.getUserName()) {
            criteria.andUserNameEqualTo(user.getUserName());
        }
        if (null != user.getPhone()) {
            criteria.andPhoneEqualTo(user.getPhone());
        }
        if (null != user.getNickName()) {
            criteria.andNickNameEqualTo(user.getNickName());
        }
        if (null != user.getSex()) {
            criteria.andSexEqualTo(user.getSex());
        }
        
        if (null != user.getReferer()) {
        	criteria.andRefererEqualTo(user.getReferer());
        }
        
        List<User> users = this.userMapper.selectByExample(userExample);

        return users;
    }
    @Override
    public List<User> selectAllUsers(User user){
    	UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        
        if (null != user.getUserName()) {
            criteria.andUserNameEqualTo(user.getUserName());
        }
        if (null != user.getPhone()) {
            criteria.andPhoneEqualTo(user.getPhone());
        }
        if (null != user.getUserId()) {
        	criteria.andUserIdEqualTo(user.getUserId());
        }
        
        if (null != user.getReferer()) {
        	criteria.andRefererEqualTo(user.getReferer());
        }
        
        List<User> users = this.userMapper.selectByExample(userExample);
        return users;
    }
    @Override
    public ResultModel addUserByPhone(User user) {
    	System.out.println(user.getPassWord());
    	System.out.println(user.getPhone());
        if (null == user.getPassWord() ||  null == user.getPhone()
        		|| user.getPassWord().trim().length()==0 || user.getPhone().trim().isEmpty()) {
            return ResultModel.error(ResultStatus.DATA_NOT_NULL);
        }

        User newUser = this.selectByPhone(user.getPhone());
        if (null != newUser) {
            return ResultModel.error(ResultStatus.USER_HAS_EXISTS);
        }

        // 设置默认昵称为 ""
        if (null == user.getNickName()) {
            user.setNickName(user.getPhone());
        }
        if (null == user.getMailNum()) {
            user.setMailNum("");
        }
        if (null == user.getUserName()) {
            user.setUserName("");
        }

        // 设置默认性别为 MAN
        if (null == user.getSex()) {
            user.setSex("MAN");
        }
   
        //设置支付密码
        if (null == user.getPayPassword()) {
        	user.setPayPassword("");
        }
        
        //设置用户头像
        if (null == user.getPicUrl()) {
        	user.setPicUrl("img/userdefalutpic.png");
        }
        if(user.getFlag()==null) {
        	user.setFlag(-1);
        }
        if(user.getStatus()==null) {
        	user.setStatus(0);
        }
        if(user.getCreateDate() == null) {
        	user.setCreateDate(new Date());
        }
        
        CodeProcess codeProcess = new CodeProcess();
        //设置推荐人
        if (null == user.getReferer()) {
        	if (null != user.getAvatar()){
        		
        		if (user.getAvatar().equals(Constants.DEFAULT_REFER_CODE)) {
        			user.setReferer(Constants.DEFAULT_REFER_ID);
        		}else {
        			//设置对应的推荐人--推荐人没有做验证
            		user.setReferer(codeProcess.decode(user.getAvatar()));
            		//判断验证码是否在数据库中存在
            		
        		}
        		
        	}else {
        		//设置默认的推荐用户ID
        		user.setReferer(Constants.DEFAULT_REFER_ID);

        	}
        	
        }
        //设置钱包
        WalletProcess walletProcess = new WalletProcess();
        WalletAccount  walletAccount = walletProcess.GetWalletAccount();
        WalletProcess walletProcess2 = new WalletProcess();
      WalletAccount  DFCwalletAccount = walletProcess2.GetDFCWalletAccount();
        if(walletAccount.getResult()) {
        	user.setWalletEthAddress(walletAccount.getWalletEthAddress());
        	user.setWalletEthPrivatekey(walletAccount.getWalletEthPrivatekey());
        	user.setWalletBtcAddress(DFCwalletAccount.getWalletBtcAddress());
        	user.setWalletBtcPrivatekey(DFCwalletAccount.getWalletBtcPrivatekey());
        	user.setWalletEosAddress(walletAccount.getWalletEosAddress());
        }else {
        	System.out.println("SERVER_INTERNAL_ERROR");
        	return ResultModel.error(ResultStatus.SERVER_INTERNAL_ERROR);
        }
        int n=this.userMapper.insert(user);
        System.out.println("添加用户:"+n);
        user = this.selectByPhone(user.getPhone());
        //设置推荐吗
        String code = codeProcess.encode(user.getUserId());
        user.setAvatar(code);
        this.userMapper.updateByPrimaryKey(user);
        
        //update
        //去除敏感信息
        user.setPassWord("");
        user.setPayPassword("");
        user.setIdcardUrla("");
        user.setIdcardUrlb("");
        user.setWalletBtcPrivatekey("");
        user.setWalletEthPrivatekey("");
        System.out.println(user);
        return ResultModel.ok(user);
    }
    
    @Override
    public ResultModel addUserByEmail(User user) {
    	System.out.println(user.getPassWord());
    	System.out.println(user.getMailNum());
        if (null == user.getPassWord() ||  null == user.getMailNum()
        		|| user.getMailNum().trim().length()==0 || user.getPassWord().trim().isEmpty()) {
            return ResultModel.error(ResultStatus.DATA_NOT_NULL);
        }

        User newUser = this.selectByEmail(user.getMailNum());
        if (null != newUser) {
            return ResultModel.error(ResultStatus.USER_HAS_EXISTS);
        }
        
        
        if (null == user.getPhone()) {
            user.setPhone("");
        }
        if (null == user.getUserName()) {
            user.setUserName("");
        }
        // 设置默认昵称为 ""
        if (null == user.getNickName()) {
            user.setNickName(user.getMailNum());
        }

        // 设置默认性别为 MAN
        if (null == user.getSex()) {
            user.setSex("MAN");
        }
   
        //设置支付密码
        if (null == user.getPayPassword()) {
        	user.setPayPassword("");
        }
        
        //设置用户头像
        if (null == user.getPicUrl()) {
        	user.setPicUrl("img/userdefalutpic.png");
        }
        if(user.getFlag()==null) {
        	user.setFlag(-1);
        }
        if(user.getStatus()==null) {
        	user.setStatus(0);
        }
        if(user.getCreateDate() == null) {
        	user.setCreateDate(new Date());
        }
        
        
        CodeProcess codeProcess = new CodeProcess();
        //设置推荐人
        if (null == user.getReferer()) {
        	if (null != user.getAvatar()){
        		
        		if (user.getAvatar().equals(Constants.DEFAULT_REFER_CODE)) {
        			user.setReferer(Constants.DEFAULT_REFER_ID);
        		}else {
        			//设置对应的推荐人--推荐人没有做验证
            		user.setReferer(codeProcess.decode(user.getAvatar()));
            		//判断验证码是否在数据库中存在
            		
        		}
        		
        	}else {
        		//设置默认的推荐用户ID
        		user.setReferer(Constants.DEFAULT_REFER_ID);

        	}
        	
        }
        
        //设置钱包
        WalletProcess walletProcess = new WalletProcess();
        WalletAccount  walletAccount = walletProcess.GetWalletAccount();
        if(walletAccount.getResult()) {
        	user.setWalletBtcAddress(walletAccount.getWalletBtcAddress());
        	user.setWalletBtcPrivatekey(walletAccount.getWalletBtcPrivatekey());
        	user.setWalletEthAddress(walletAccount.getWalletEthAddress());
        	user.setWalletEthPrivatekey(walletAccount.getWalletEthPrivatekey());
        	user.setWalletEosAddress(walletAccount.getWalletEosAddress());
        }else {
        	return ResultModel.error(ResultStatus.SERVER_INTERNAL_ERROR);
        }
        
        this.userMapper.insert(user);

        user = this.selectByEmail(user.getMailNum());
        //设置推荐吗
        String code = codeProcess.encode(user.getUserId());
        user.setAvatar(code);
        this.userMapper.updateByPrimaryKey(user);
        
        //update
        //去除敏感信息
        user.setPassWord("");
        user.setPayPassword("");
        user.setIdcardUrla("");
        user.setIdcardUrlb("");
        user.setWalletBtcPrivatekey("");
        user.setWalletEthPrivatekey("");
        return ResultModel.ok(user);
    }

    @Override
    public ResultModel deleteUser(int userId) {
        User user = this.userMapper.selectByPrimaryKey(userId);

        if (null == user) {
            return ResultModel.error(ResultStatus.USER_NOT_FOUND);
        }

        this.userMapper.deleteByPrimaryKey(userId);
        return ResultModel.ok();
    }

    @Override
    public ResultModel updateUser(int userId, User user) {
        User newUser = this.userMapper.selectByPrimaryKey(userId);

        // 找不到 user
        if (null == newUser) {
            return ResultModel.error(ResultStatus.USER_NOT_FOUND);
        }

        if (null != user.getPassWord()) {
            newUser.setPassWord(user.getPassWord());
        }
        if (null != user.getPhone()) {
            newUser.setPhone(user.getPhone());
        }
        if (null != user.getMailNum()) {
            newUser.setMailNum(user.getMailNum());
        }
        if (null != user.getSex()) {
            newUser.setSex(user.getSex());
        }
        if (null != user.getNickName()) {
            newUser.setNickName(user.getNickName());
        }
      
        if(null != user.getPayPassword()) {
        	newUser.setPayPassword(user.getPayPassword());
        }
        if(null != user.getPicUrl()) {
        	newUser.setPicUrl(user.getPicUrl());
        }
        if(null != user.getIdcardUrla()) {
        	newUser.setIdcardUrla(user.getIdcardUrla());
        }
        if(null != user.getIdcardUrlb()) {
        	newUser.setIdcardUrlb(user.getIdcardUrlb());
        }
        if(user.getFlag() != null){
        	newUser.setFlag(user.getFlag());
        }
        if(user.getStatus() != null){
        	newUser.setStatus(user.getStatus());
        }
        if(user.getUserName() != null) {
        	newUser.setUserName(user.getUserName());
        }
        if(user.getCreateDate() != null) {
        	newUser.setCreateDate(user.getCreateDate());
        }
        
        System.out.println("before updateByPrimaryKey ===>" + newUser );
        this.userMapper.updateByPrimaryKey(newUser);
        newUser.setPassWord("");
        newUser.setPayPassword("");
        newUser.setIdcardUrla("");
        newUser.setIdcardUrlb("");
        newUser.setWalletBtcPrivatekey("");
        newUser.setWalletEthPrivatekey("");
        return ResultModel.ok(newUser);
    }

    @Override
    public ResultModel updateAvatar(int userId, String avatar) {
        User user = this.userMapper.selectByPrimaryKey(userId);

        if (null == user) {
            return ResultModel.error(ResultStatus.USER_NOT_FOUND);
        }

        user.setAvatar(avatar);
        this.userMapper.updateByPrimaryKey(user);
        return ResultModel.ok();
    }
    
    @Override
    public User getUserByPhoneAndLoginPwd(String phone,String loginPwd) {
    	UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andPhoneEqualTo(phone);
        criteria.andPassWordEqualTo(loginPwd);
        List<User> users = this.userMapper.selectByExample(userExample);
        System.out.println("phone = "+phone+"  loginPwd = "+loginPwd);
        System.out.println(users.toString());
        if (users == null|users.size() == 0) {
        	return null;
		}else {
			return users.get(0);
		}
    }
	@Override
	public List<User> getUserIDCard(Integer flag,Integer page,Integer rows,String phone,Integer userId) {
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		 UserExample userExample = new UserExample();
		 UserExample.Criteria criteria = userExample.createCriteria();
		 if(phone != null && phone.trim().length() != 0) {
			 criteria.andPhoneEqualTo(phone);
		 }
		 if(userId != null) {
			 criteria.andUserIdEqualTo(userId);
		 }
		 if(flag != null) {
			 criteria.andFlagEqualTo(flag);
		 }
	     userExample.setOrderByClause("`create_date` DESC");
	     List<User> users = this.userMapper.selectByExample(userExample);
		return users;
	}
	@Override
	public int totalPages(Integer flag, Integer rows,String phone,Integer userId) {
		 UserExample userExample = new UserExample();
		 UserExample.Criteria criteria = userExample.createCriteria();
		 if(phone != null &&  !phone.equals("")) {
			 criteria.andPhoneEqualTo(phone);
		 }
		 if(userId != null) {
			 criteria.andUserIdEqualTo(userId);
		 }
		 if(flag != null) {
			 criteria.andFlagEqualTo(flag);
		 }
		 long total=this.userMapper.countByExample(userExample);
		 int totalPages;
			if(total%rows==0) {
				totalPages=(int) (total/rows);
			}else {
				totalPages=(int) (total/rows)+1;
			}
			return totalPages;
	}
	
	@Override
	public List<NodeItem> getTwoGeneration(Integer userId) {
		List<NodeItem> userList=new ArrayList<NodeItem>();
		User user = new User();
        user.setReferer(userId);
        List<User> users = selectAllUsers(user);
        for(User u1 : users) {
        	NodeItem nodeItem = new NodeItem();
        	Integer child1=u1.getUserId();
        	nodeItem.setUserId(child1);
        	nodeItem.setLevelIndex(1);
        	userList.add(nodeItem);
        	
        	User user1 = new User();
            user1.setReferer(child1);
            List<User> users1 = selectAllUsers(user1);
        	for(User u2 : users1) {
        		NodeItem nodeItem2 = new NodeItem();
        		Integer child2=u2.getUserId();
        		nodeItem2.setUserId(child2);
        		nodeItem2.setLevelIndex(2);
        		userList.add(nodeItem2);
        	}
        }
        for(NodeItem node: userList) {
        	System.out.println("$$$=>"+node.getUserId());
        	System.out.println("###=>"+node.getLevelIndex());
        }
		return userList;
	}
	

	@Override
	public int realTotal(List<NodeItem> userIdList) {
		int index=0;
		for(NodeItem node : userIdList) {
			User user = selectByUserId(node.getUserId());
			int flag=user.getFlag();
			if(flag == 1) {
				index++;
			}
		}
		System.out.println("实名index=>"+index);
		return index;
	}
	@Override
	public List<Integer> selectAlluserId() {
		List<Integer>list=this.userMapper.selectAlluserId();
		return list;
	}
	
	@Override
	public int selectLeaderIdByuserId(Integer userId) {
		User user=this.selectByUserId(userId);
		int referer=user.getReferer();
		if(referer == 0) {
			return referer;
		}
		while(referer != 0) {
			user=this.selectByUserId(referer);
			referer=user.getReferer();
		}
		return user.getUserId();
	}
	@Override
	public int getUserStart(Integer userId) {
		// TODO Auto-generated method stub
		//进行获取该用户的所有直推用户的投资额度
		double userReferAsset = 0.0;
		User user = new User();
        user.setReferer(userId);
        List<User> users = selectAllUsers(user);
        for(User u1 : users) {
        	Integer child1=u1.getUserId();
        	//userReferAsset += this.cbtInvestService.getInvestById(id)
        	List<CbtInvest> cbtInvestList = this.cbtInvestMapper.selectByuserId(child1);
        	for (CbtInvest cbtInvest : cbtInvestList) {
        		userReferAsset += cbtInvest.getPrice()*cbtInvest.getEthPrice();
			}	
        }
        //进行获取用户的实际投资额度
//        userReferAsset = userReferAsset/Constants.AKB_INVEST_RATIO;
        return this.cbtMinerRatioService.getUserLevelByAsset(userReferAsset);
	}
	
	@Override
	public  double getRewardValue(NodeItem item, int rootUserStar, int maxUserStar, boolean isRoot) {
        // TODO Auto-generated method stub
        double assetIncome = 0.0;

        LinkedList<NodeItem> tmpItem = item.getChildList();
        //开始遍历
        for (NodeItem nodeItem : tmpItem) {
            //如果是根节点 就首先计算出当前节点的直推返利
            //当前节点直推收益计算规则: 按照当前节点等级 获取返利百分比
            if (isRoot) {
                CbtStarLevel cbtStarLevel = this.cbtStarLevelService.selectUserStarLevelByStar(rootUserStar);
                //计算出当前节点直推数量
                double asset = nodeItem.getAssetValidInvest();
                //添加到总金额中
                assetIncome += asset * (cbtStarLevel.getRatio());

                //获取到子级的等级
                if (nodeItem.getUserStar() >= rootUserStar) {
                    //如果大于或者等于父类节点 就停止递归当前节点之下所有叶子
                    continue;
                } else {
                    //如果小于父类节点 就开始带入当前节点的等级 继续递归
                    assetIncome += getRewardValue(nodeItem, rootUserStar, nodeItem.getUserStar(), false);
                }
            } else {
                //首先判断当前根节点 是否小于或者等于当前节点
                if (nodeItem.getUserStar() >= rootUserStar) {
                    //如果大于或者等于父类节点 就停止递归当前节点之下所有叶子
                    continue;
                } else {
                    //如果小于当前节点 就继续计算出当前节点的能够返回的利润
                    //首先计算root节点与当前直接节点最大的差值是多少
                    CbtStarLevel cbtStarLevel1 = this.cbtStarLevelService.selectUserStarLevelByStar(rootUserStar);
                    CbtStarLevel cbtStarLevel2 = this.cbtStarLevelService.selectUserStarLevelByStar(maxUserStar);
                    double asset = nodeItem.getAssetValidInvest();
                    assetIncome += asset * (cbtStarLevel1.getRatio() - cbtStarLevel2.getRatio());

                    //比对当前最大节点信息
                    //继续递归下级
                    if (nodeItem.getUserStar() >= maxUserStar) {
                        assetIncome += getRewardValue(nodeItem, rootUserStar, nodeItem.getUserStar(), false);
                    }else {
                        assetIncome += getRewardValue(nodeItem, rootUserStar, maxUserStar, false);
					}
                 
               }
            }
        }
        return assetIncome;
    }
	
//	@Override
//	public double getRewardValue(NodeItem nodeItem, int rootUserStar,boolean isFull, boolean isRoot, int maxTreeStar,boolean overStepFlag) {
//		// TODO Auto-generated method stub
//		double assetIncome = 0.0;
//		CbtStarLevel cbtStarLevel = this.cbtStarLevelService.selectUserStarLevelByStar(rootUserStar);
//	
//		
//		LinkedList<NodeItem> tmpItem = nodeItem.getChildList();
//		for (NodeItem nodeItem2 : tmpItem) {
//			if(nodeItem2.getUserStar() > rootUserStar) {
//				continue;
//				
//			}else if (nodeItem2.getUserStar() == rootUserStar) {
//				
////				if (overStepFlag) {
////				
////					assetIncome +=getRewardValue(nodeItem2,nodeItem2.getUserStar(),isFull,true,0,false);
////					
////				}
//				continue;
//				
//			}else {
//				
//				int realChildStar = (maxTreeStar >=nodeItem2.getUserStar())?maxTreeStar:nodeItem2.getUserStar();
//				CbtStarLevel childStarLevel = this.cbtStarLevelService.selectUserStarLevelByStar(realChildStar);
////				double asset = (isFull == true)?nodeItem2.getTeamAssetInvest():nodeItem2.getTeamAssetValidInvest();
//				double asset = nodeItem2.getAssetValidInvest();
//				System.out.println("小于的情况"+nodeItem.getUserPhone()+"*****assetIncome==="+asset * (cbtStarLevel.getRatio() - childStarLevel.getRatio()));
////				if(isRoot==false){
//					assetIncome += asset * (cbtStarLevel.getRatio() - childStarLevel.getRatio());
////				}
//				assetIncome += getRewardValue(nodeItem2,rootUserStar,isFull,false,realChildStar,overStepFlag);
//				
//				}
//		}
//		
//		System.out.println("最终值"+nodeItem.getUserPhone()+"*****assetIncome==="+assetIncome);
//		return assetIncome;
//	}
//	@Override
//	public double getRewardValue(NodeItem nodeItem, int rootUserStar, boolean isFull, boolean isRoot) {
//		// TODO Auto-generated method stub
//		double assetIncome = 0.0;
//		CbtStarLevel cbtStarLevel = this.cbtStarLevelService.selectUserStarLevelByStar(rootUserStar);
//		int nodeStar = nodeItem.getUserStar();
//		LinkedList<NodeItem> tmpItem = nodeItem.getChildList();		
//		for (NodeItem nodeItem2 : tmpItem) {
//			//重新进行梳理逻辑
//			//简化规则,由于当前参与计算的数据值,全部是通过截断后的数据
//			//只需要和根节点进行比较,如果大于根节点,则直接返回,如果等于根节点,则计算收益的20%,如果小于根节点,则进行直接计算即可;
//			//为了去重,有节点的总体收益重复计算,增加如下规则,如果当前节点小于根节点话,则不进行参与计算
//			if(nodeItem2.getUserStar() > rootUserStar) {
//				//截断返回
//				continue;
//			}else if (nodeItem2.getUserStar() == rootUserStar) {
//				//如果平级截断的话,则进行计算下一个评级节点的收益信息的20%
////				double asset = (isFull == true)?nodeItem2.getTeamAssetInvest():nodeItem2.getTeamAssetValidInvest();
////				CbtStarLevel childStarLevel = this.cbtStarLevelService.selectUserStarLevelByStar(nodeItem2.getUserStar());
////				assetIncome += asset * childStarLevel.getRatio()* 0.2;
//				//平级截断后,按照新的节点进行计算,进行获取新节点的推荐收益信息
//				assetIncome += getRewardValue(nodeItem2,nodeItem2.getUserStar(),isFull,true) * 0.2;
//			}else {
//				if (isRoot) {
//					CbtStarLevel childStarLevel = this.cbtStarLevelService.selectUserStarLevelByStar(nodeItem2.getUserStar());
//					double asset = (isFull == true)?nodeItem2.getTeamAssetInvest():nodeItem2.getTeamAssetValidInvest();
//					//则进行计算当前节点的信息
//					
//					assetIncome +=  asset * (cbtStarLevel.getRatio() - childStarLevel.getRatio());
//					//进行计算下一个节点
//					assetIncome += getRewardValue(nodeItem2,rootUserStar,isFull,false);
//				}
//				else {
//					if (nodeStar == nodeItem2.getUserStar()) {
//						//如果该节点小于根节点的同时,又小于它的上级节点
//						double asset = (isFull == true)?nodeItem2.getTeamAssetInvest():nodeItem2.getTeamAssetValidInvest();
//						CbtStarLevel childStarLevel = this.cbtStarLevelService.selectUserStarLevelByStar(nodeItem2.getUserStar());
//						
//						assetIncome += asset * childStarLevel.getRatio()* 0.2;
//						assetIncome +=getRewardValue(nodeItem2,rootUserStar,isFull,false);
//					}else  if (nodeItem2.getUserStar() > nodeStar) {
//						CbtStarLevel childStarLevel = this.cbtStarLevelService.selectUserStarLevelByStar(nodeItem2.getUserStar());
//						double asset = (isFull == true)?nodeItem2.getTeamAssetInvest():nodeItem2.getTeamAssetValidInvest();
//						assetIncome += asset * (cbtStarLevel.getRatio() - childStarLevel.getRatio());
//						assetIncome += getRewardValue(nodeItem2,rootUserStar,isFull,false);
//					} 
//					else{
//						//如果该节点小于根节点的同时,又小于他的上级节点
//						//在该节点小于根节点的同时,又小于它的上级节点话,则不进行做额外的计算
//						assetIncome += getRewardValue(nodeItem2,rootUserStar,isFull,false);
//					}
//					
//				}
//			}
//		}
//		BigDecimal decimal = new BigDecimal(Double.toString(assetIncome));
//        System.out.println(decimal);
//        BigDecimal number = decimal.setScale(4,BigDecimal.ROUND_DOWN).stripTrailingZeros();
//		return number.doubleValue();
//	}
	
	public List<User> selectUsersListbyUserId(Integer userId){
		List<User> list = userMapper.selectUsersListbyUserId(userId);
		return list;
	}
	
	@Override
	public User selectByPhoneAndCode(String phone, String area) {
		// TODO Auto-generated method stub
	 	UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andPhoneEqualTo(phone);
        List<User> users = this.userMapper.selectByExample(userExample);
        User user;
        try {
        	CbtAreaExample cbtAreaExample = new CbtAreaExample();
        	CbtAreaExample.Criteria criteria2 = cbtAreaExample.createCriteria();
        	criteria2.andUserIdEqualTo(users.get(0).getUserId());
        	criteria2.andAccountEqualTo(area);
        	List<CbtArea> selectByExample = cbtAreaMapper.selectByExample(cbtAreaExample);
        	if (selectByExample.size()>0) {
        		 user = users.get(0);
			}else {
				return null;
			}
        } catch (Exception e) {
            return null;
        }
        return user;
		
	}
	
	@Override
	public int totalAllPages( Integer rows) {
		 UserExample userExample = new UserExample();
		 UserExample.Criteria criteria = userExample.createCriteria();
		
		 long total=this.userMapper.countByExample(userExample);
		 int totalPages;
			if(total%rows==0) {
				totalPages=(int) (total/rows);
			}else {
				totalPages=(int) (total/rows)+1;
			}
			return totalPages;
	}

}
